﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;
using Bohdan.Arch.Messaging;

namespace Bohdan.Arch.Messaging.Special
{
    public class TimeMeasuredMessage : GeneralMessage
    {
        protected GeneralMessage _MeasuredMessage;
        public GeneralMessage MeasuredMessage
        {
            get
            {
                return _MeasuredMessage;
            }
        }
        protected Stopwatch _stopwatch;

        public TimeMeasuredMessage(GeneralMessage msg)
        {
            _MeasuredMessage = msg;
            _stopwatch = Stopwatch.StartNew();
            msg.OnMessageProcessed += new EventHandler(msg_OnMessageProcessed);
        }

        public override Guid TargetSubscriberId
        {
            get
            {
                return TimeMeasurerSubscriber._Id;
            }
        }

        void msg_OnMessageProcessed(object sender, EventArgs e)
        {
            _stopwatch.Stop();
#if TRACE_MESSAGES
            Trace.TraceInformation("Message {0} has been processing (after queue) for {1} ms.", MeasuredMessage.ToString(), _stopwatch.ElapsedMilliseconds);
#endif
        }

    }
}
